package controllers;

import helpers.CrawlerHelper;
import helpers.FileHelper;
import helpers.UrlHelper;

import java.io.IOException;
import java.net.URLDecoder;
import java.util.Collections;
import java.util.TreeMap;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.cache.Cache;
import javax.cache.CacheException;
import javax.cache.CacheManager;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import systems.CurrentDomain;

import debugs.DomainDebug;

import entities.Domain;

public class WhoisServlet extends HttpServlet {
	
	private static final Logger log = Logger.getLogger(WhoisServlet.class.getName());
	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
		throws IOException, ServletException {
		log.setLevel(Level.FINE);
		
		TreeMap<String, String> check = new TreeMap<String, String>();
		Domain domain = new Domain();
		
		String path = ((HttpServletRequest) request).getRequestURI();
		String name = URLDecoder.decode(path.toLowerCase().replaceAll(request.getServletPath() + "/", ""),"UTF-8");
		
		String keywords = "whois, whois data, whois domain, domain whois, whois services, show whois, whois information, whois info";
		String description = "Provide full service whois information for domain.";
		
		//Convert to main domain
		if (name != null && !name.isEmpty()) {
			name = UrlHelper.getMainDomain(name);
			domain.setDomain(name);
			if (!UrlHelper.checkDomain(name)) {
				check.put("name", name + " is not a domain. Please enter again!");
			}
			
			if (check.size() == 0) {
				//Check domain exists db
				domain = FileHelper.getDomainByFileName(name);
				if (domain == null) {
					domain = new Domain();
					domain.setDomain(name);
					
					String whois = CrawlerHelper.getWhois(name);
					if (whois != null && !whois.isEmpty()) {
						domain.setWhois(whois);
						FileHelper.savePage(domain);
						keywords = name + ", whois data, whois info, whois Registrant, whois domain Expiration, whois administrative contact, whois billing content";
						description = "Data whois for " + name + ": whois info, whois registranct, whois domain expiration, whois administrator contact, whois billing";
						log.info("Update success new domain: " + name);
					} else {
						keywords = "whois, avaiable, register, free domain, whois info";
						description = name + " domain can avaiable register.";
						log.warning("Not exists domain: " + name);
					}
				} else {
					keywords = name + ", whois data, whois info, whois Registrant, whois domain Expiration, whois administrative contact, whois billing content";
					description = "Data whois for " + name + ": whois info, whois registranct, whois domain expiration, whois administrator contact, whois billing";
					log.info("Get domain " + name + " from database");
				}
				
				CurrentDomain.insert(name);
			}
		} else {
			response.sendRedirect("/");
			return;
		}
		
		request.setAttribute("domain", domain);
		request.setAttribute("check", check);
		request.setAttribute("title", name +  " - whois data." );
		request.setAttribute("keywords", keywords);
		request.setAttribute("description", description);
		try {
			request.getRequestDispatcher("/pages/home.jsp").forward(request, response);
		} catch (ServletException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
